Ext.ns('ru.proburo.oms.order.components.form');

/**
 * @class ru.proburo.oms.order.components.form.Spinner
 * @extends Ext.form.NumberField
 */
ru.proburo.oms.order.components.form.Spinner = Ext.extend(Ext.form.NumberField, {
    
    actionMode: 'wrap',
    deferHeight: true,
    //autoSize: Ext.emptyFn,
    //onBlur: Ext.emptyFn
    
    constructor: function(config)
	{		
		config = config || {};
        
        Ext.applyIf(config, {
            allowNegative: 	  false,
            allowDecimals: 	  true,
			allowBlank: 	  false,
			minValue: 		  1.00,
			decimalPrecision: 2,			
            // Spinner conf
            accelerate: 	  true,
            multiplicity:     1
        });
        
        var spl = this.spinner = new Ext.ux.Spinner(Ext.copyTo({}, config, [
            'incrementValue',
            'alternateIncrementValue',
            'accelerate',
            'defaultValue',
            'triggerClass',
            'splitterClass'
        ]));
        
        if(config.plugins)
        {
            if(Ext.isArray(config.plugins))
            {
                config.plugins.push(spl);
            }
            else
            {
                config.plugins = [config.plugins, spl];
            }
        }
        else
        {
            config.plugins = spl;
        }
        
        ru.proburo.oms.order.components.form.Spinner.superclass.constructor.call(this, config);
    },

    validateBlur: function(){ return true; },
    adjustSize: Ext.BoxComponent.prototype.adjustSize,
	// private
    getResizeEl: function(){ return this.wrap; },
    // private
    getPositionEl: function(){ return this.wrap; },
    // private
    alignErrorIcon: function()
    {
        if(this.wrap)
        {
            this.errorIcon.alignTo(this.wrap, 'tl-tr', [2, 0]);
        }
    },
    
    initComponent: function()
    {
        ru.proburo.oms.order.components.form.Spinner.superclass.initComponent.call(this);
        
        //this.setMultiplicity(this.multiplicity);
        
        this.on('show', function(){
            //console.log('F*ck!');
            //this.markInvalid('F*ck!');
        }, this);
    },
    
    
    
    getMultiplicity: function()
    {
        return this.incrementValue;
    },
    
    setMultiplicity: function(multiplicity)
    {
        this.incrementValue          = multiplicity;
        this.alternateIncrementValue = multiplicity * 4;
        
        return this;
    },
    
    fixMultiplicity: function(value)
    {
        if(0 !== (rem = value % this.multiplicity))
        {
            value -= rem;
            
            if(rem >= this.multiplicity/2)
            {
                value += this.multiplicity;
            }
        }
        
        return value;
    }
});